package com.business.api.official.toursism.websiteshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websiteshop.dto.WebsiteShopApiDTO;
import com.business.api.official.toursism.websiteshop.from.WebsiteShopApiSaveFrom;
import com.business.api.official.toursism.websiteshop.from.WebsiteShopApiUpdateFrom;
import com.business.api.official.toursism.websiteshop.manager.WebsiteShopApiManager;
import com.business.api.official.toursism.websiteshop.query.WebsiteShopApiPageQuery;
import com.business.api.official.toursism.websiteshop.query.WebsiteShopApiQuery;
import com.business.api.official.toursism.websiteshop.service.WebsiteShopApiService;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiBriefPageVO;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiDetailListVO;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiDetailPageVO;
import com.business.api.official.toursism.websiteshop.vo.data.WebsiteShopApiBriefData;
import com.business.api.official.toursism.websiteshop.vo.data.WebsiteShopApiDetailData;
import com.business.system.official.toursism.websiteshop.entity.WebsiteShop;
import com.business.system.official.toursism.websiteshop.service.WebsiteShopService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description 购物车
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@Service("websiteShopApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteShopApiServiceImpl extends SeedApiServiceImpl<WebsiteShopApiManager, WebsiteShop> implements WebsiteShopApiService {

    @Autowired
    private WebsiteShopService systemService;


    /**
     * 持久化
     *
     * @param websiteShopFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteShopApiDTO save(WebsiteShopApiSaveFrom websiteShopFrom) throws ServiceException {
        WebsiteShopApiDTO apiDTO = new WebsiteShopApiDTO();
        WebsiteShop websiteShop = new WebsiteShop();
        try {
            BeanUtils.copyProperties(websiteShopFrom, websiteShop, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteShopFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteShop);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteShop(websiteShop);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteShopFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteShopApiDTO updateSelectiveById(WebsiteShopApiUpdateFrom websiteShopFrom) throws ServiceException {
        WebsiteShopApiDTO apiDTO = new WebsiteShopApiDTO();
        if (StringUtils.isEmpty(websiteShopFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteShop websiteShop = new WebsiteShop();
        try {
            BeanUtils.copyProperties(websiteShopFrom, websiteShop);
        } catch (Exception e) {
            throw new ServiceException(e, websiteShopFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteShop);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteShopApiDetailListVO listAll(WebsiteShopApiQuery query) throws ServiceException {
        WebsiteShop queryEntity = new WebsiteShop();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteShop> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteShop> list = this.list(lambdaQueryWrapper);
        List<WebsiteShopApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteShopApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteShopApiBriefPageVO getBriefPageVO(WebsiteShopApiPageQuery query) throws ServiceException {
        Page<WebsiteShop> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteShop queryEntity = new WebsiteShop();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteShop> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteShop> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteShop> list = pageData.getRecords();
        List<WebsiteShopApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteShopApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteShopApiDetailPageVO getDetailPageVO(WebsiteShopApiPageQuery query) throws ServiceException {
        Page<WebsiteShop> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteShop queryEntity = new WebsiteShop();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteShop> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteShop> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteShop> list = pageData.getRecords();
        List<WebsiteShopApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteShopApiDetailPageVO(page, voList);
    }

}